/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "HVector3.h"

namespace hhd
{
	const HVector3& HVector3::zero = _mm_setzero_ps();
	const HVector3& HVector3::unit_x = _mm_set_ps(1.0f, 0.0f, 0.0f, 0.0f);
	const HVector3& HVector3::unit_y = _mm_set_ps(0.0f, 1.0f, 0.0f, 0.0f);
	const HVector3& HVector3::unit_z = _mm_set_ps(0.0f, 0.0f, 1.0f, 0.0f);

	HVector3::HVector3(void)
	{
		_data = _mm_setzero_ps();
	}


	HVector3::~HVector3(void)
	{
	}

	HVector3::HVector3(hfloat32 x, hfloat32 y, hfloat32 z)
	{
		_data = _mm_setr_ps( x, y, z, 0.0f );
	}

	HVector3::HVector3(const HVector3& source)
	{
		_data = source._data;
	}

	HVector3::HVector3(const __m128& source)
	{
		_data = source;
	}

	HVector3& HVector3::operator=(const HVector3& other)
	{
		_data = other._data;
		return *this;
	}

	HVector3& HVector3::operator=(const __m128& source)
	{
		_data = source;
		return *this;
	}

	HVector3 HVector3::operator*(hfloat32 scalar)
	{
		__m128 filled = _mm_load1_ps( &scalar );
		return _mm_mul_ps( _data, filled );
	}

	HVector3 operator*(hfloat32 scalar, HVector3& vector)
	{
		__m128 filled = _mm_load1_ps( &scalar );
		return _mm_mul_ps( vector._data, filled );
	}

	HVector3 HVector3::operator*(const HVector3& other)
	{
		return _mm_mul_ps( _data, other._data );
	}

	HVector3::operator const __m128&() const
	{
		return _data;
	}

	HVector3& HVector3::operator*=(hfloat32 scalar)
	{
		__m128 filled = _mm_load1_ps( &scalar );
		_data = _mm_mul_ps( _data, filled );
		return *this;
	}

	HVector3 HVector3::operator+(const HVector3& to_add)
	{
		return _mm_add_ps( _data, to_add._data );
	}

	HVector3 HVector3::operator-(const HVector3& to_subtrace)
	{
		return _mm_sub_ps( _data, to_subtrace._data );
	}

	HVector3& HVector3::operator+=(const HVector3& to_add)
	{
		_data = _mm_add_ps( _data, to_add._data );
		return *this;
	}

	bool HVector3::operator==(const HVector3& other)
	{
		//get comparison
		__m128 comparison = _mm_cmpeq_ps( _data, other._data );
		//use mask comparison
		int mask = _mm_movemask_ps( comparison );
		return (mask & 7) == 7;
	}

	bool HVector3::operator!=(const HVector3& other)
	{
		//get comparison
		__m128 comparison = _mm_cmpneq_ps( _data, other._data );
		//use mask comparison
		int mask = _mm_movemask_ps( comparison );
		return (mask & 7) == 7;
	}

	hfloat32 HVector3::dot(const HVector3& one, const HVector3& two)
	{
		__m128 accum;
		__m128 mul = _mm_mul_ps( one._data, two._data );

		accum = _mm_add_ps( mul, _mm_shuffle_ps( mul, mul, _MM_SHUFFLE( 3, 1, 0, 2 ) ) );
		accum = _mm_add_ps( accum, _mm_shuffle_ps( mul, mul, _MM_SHUFFLE( 3, 0, 2, 1 ) ) );

		float result;

		_mm_store_ss( &result, accum );
		return result;
	}
}